# This file contains the fastlane.tools configuration
# You can find the documentation at https://docs.fastlane.tools
#
# For a list of all available actions, check out
#
#     https://docs.fastlane.tools/actions
#
# For a list of all available plugins, check out
#
#     https://docs.fastlane.tools/plugins/available-plugins
#
opt_out_usage
default_platform(:android)

platform :android do
  def ensure_git_ok()
    ensure_git_branch(branch: "beta")
    ensure_git_status_clean
  end
  def gradle_reproducible(options)
    gradle(flags: "--no-configuration-cache --no-build-cache --no-daemon",
           system_properties: options[:system_properties],
           tasks: ["clean", options[:task]])
  end

  desc "Runs all the tests"
  lane :test do
    gradle(task: "test")
  end

  desc "Deploy a new version to the Google Play"
  lane :googleplay do
    ensure_git_ok
    gradle_reproducible(system_properties: {"org.gradle.project.signing2": "true", "org.gradle.project.releaseType": "Release"},
           task: ":app:bundleRelease")
    upload_to_play_store(release_status: "draft", track: "beta", skip_upload_apk: true)
  end

  desc "Build release"
  lane :buildrel do
    ensure_git_ok
    
    gradle_reproducible(system_properties: {"org.gradle.project.releaseType": "Release"},
             task: ":app:assembleRelease")
  end

  desc "Build & push release to Git"
  lane :gitrel do
    buildrel
    vercode = android_get_version_code(gradle_file: "app/build.gradle.kts")
    version = android_get_version_name(gradle_file: "app/build.gradle.kts")
    changelog = File.read("metadata/android/en-US/changelogs/#{vercode}.txt")
    set_github_release(
      repository_name: "FoedusProgramme/Gramophone",
      api_token: File.read("creds.txt"), # make sure it has no newline :D has to contain personal access token
      name: "Gramophone #{version}",
      tag_name: version, # must be version name for F-Droid
      is_draft: true,
      description: changelog,
      commitish: "beta",
      upload_assets: [lane_context[SharedValues::GRADLE_APK_OUTPUT_PATH]]
    )
   end

  desc "Prepare release"
  lane :preprel do
    ensure_git_ok
    orig_version = android_get_version_name(gradle_file: "app/build.gradle.kts")
    version = prompt(text: "Enter new version (current is #{orig_version})")
    changelog = prompt(text: "Enter Changelog: (type END when done)", multi_line_end_keyword: "END")
    android_set_version_code(gradle_file: "app/build.gradle.kts")
    vercode = android_get_version_code(gradle_file: "app/build.gradle.kts")
    android_set_version_name(version_name: version, gradle_file: "app/build.gradle.kts")
    File.write("metadata/android/en-US/changelogs/#{vercode}.txt", changelog)
    git_add(path: "fastlane/metadata/android/en-US/changelogs/#{vercode}.txt")
    gradle_reproducible(system_properties: {"org.gradle.project.releaseType": "Release"},
           task: ":app:generateReleaseBaselineProfile")
    git_add(path: ["app/src/release/generated/baselineProfiles/baseline-prof.txt",
                   "app/src/release/generated/baselineProfiles/startup-prof.txt"])
    git_commit(path: ["app/build.gradle.kts",
    "fastlane/metadata/android/en-US/changelogs/#{vercode}.txt",
    "app/src/release/generated/baselineProfiles/baseline-prof.txt",
    "app/src/release/generated/baselineProfiles/startup-prof.txt"], message: "Release #{version}")
    googleplay
    gitrel
    println(message: "Created draft release: #{lane_context[SharedValues::SET_GITHUB_RELEASE_HTML_LINK]}")
  end
end
